<template>
  <!-- 新增录入画面对话框-->
  <el-dialog
    :title="form.title"
    center
    :visible.sync="openState"
    width="1024px"
    :close-on-click-modal="false"
    @close="closeFn"
    :before-close="handleClose"
    v-if="openState"
  >
    <el-form ref="editForm" :model="form" :rules="rules" :disabled="form.disabled" label-width="110px">
      <el-row>
        <el-col :span="12">
          <el-form-item label="费项代码" prop="feeCode">
            <el-input v-model="form.feeCode" placeholder="" :disabled="form.isDisabled" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-row>
            <el-button
              type="primary"
              icon="el-icon-search"
              :disabled="form.isDisabled"
              plain
              @click="openPayerDialog('feeCode')"
            >
              费项查找
            </el-button>
          </el-row>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="状态" prop="chargeStatus">
            <el-select v-model="form.chargeStatus" placeholder="" style="width: 100%">
              <el-option
                v-for="dict in dictMap.FEE_CHARGE_STATUS"
                :key="dict.value"
                :label="`${dict.value}-${dict.text}`"
                :value="dict.value"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="自动扣费类型" prop="chargeAutotype">
            <el-select v-model="form.chargeAutotype" placeholder="" style="width: 100%">
              <el-option
                v-for="dict in dictMap.FEE_CHARGE_AUTOTYPE"
                :key="dict.value"
                :label="`${dict.value}-${dict.text}`"
                :value="dict.value"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
<!--      <el-row>
        <el-col :span="12">
          <el-form-item label="扣费时间" prop="chargeTime">
            <el-date-picker style="width: 100%;" v-model="form.chargeTime" value-format="yyyyMMddHHmmss"
                            type="datetime"/>
          </el-form-item>
        </el-col>
      </el-row>-->
      <el-row>
        <el-col :span="24">
          <el-form-item label="备注" prop="remarks">
            <el-input v-model="form.remarks" type="textarea" maxlength="60" show-word-limit/>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm" :loading="isLoading" v-if="form.isDetail">提交</el-button>
      <el-button type="primary" @click="reset"  v-if="form.isDetail">重置</el-button>
      <el-button type="primary" @click="closeFn" >关闭</el-button>
    </div>

    <search-dialog ref="searchDialog" @confirm="confirmPayer"></search-dialog>
  </el-dialog>

</template>
<script>
import { save,update } from "@/api/cnaps/tips/FeeItemManage/FeeWithholdingManage.js";
import SearchDialog from "./searchFeeCode.vue";
export default {
  name: "FeeWithholdingManageDialog",
  components: {SearchDialog},
  props: {
    dictMap: {
      type: Object,
      default: () => {
        return {};
      },
    },
    form: {
      type: Object,
      default: () => {
        return {};
      },
    },
  },
  computed: {},

  data() {
    return {
      disabled: false,
      isLoading: false,
      // 是否显示弹出层
      openState: false,
      // 表单校验
      rules: {
        feeCode: [{ required: true, message: "不能为空", trigger: "blur" }],
        chargeStatus: [{ required: true, message: "不能为空", trigger: "blur" }],
        chargeAutotype: [{ required: true, message: "不能为空", trigger: "change" }],
        chargeTime: [{ required: true, message: "不能为空", trigger: "blur" }],
      },
    };
  },
  mounted() {},

  methods: {
    // 表单重置
    reset() {
      this.resetForm("editForm");
    },

    openPayerDialog(fromFlag = "") {
      this.$refs.searchDialog.show(fromFlag);
    },
    confirmPayer(fromFlag, row) {
      if (fromFlag == "feeCode") {
        this.$set(this.form, "feeCode", row.feeCode);
      }
    },

    closeFn() {
      this.openState = false;
      this.reset();
      this.$set(this.form,"feeCode","");
      this.$set(this.form,"chargeStatus","");
      this.$set(this.form,"chargeAutotype","");
      this.$set(this.form,"chargeTime","");
      this.$set(this.form,"remarks","");
    },

    show() {
      this.openState = true;
    },
    submitForm() {
      this.$refs["editForm"].validate((valid) => {
        if (valid && this.form.isDisabled) {
          this.isLoading = true;
          update(this.form)
            .then(() => {
              this.isLoading = false;
              this.msgSuccess("修改成功");
              this.closeFn();
            })
            .catch((err) => {
              this.isLoading = false;
            });
        } else if (valid && !this.form.isDisabled) {
          this.isLoading = true;
          save(this.form)
            .then(() => {
              this.isLoading = false;
              this.msgSuccess("添加成功");
              this.closeFn();
            })
            .catch((err) => {
              this.isLoading = false;
            });
        }
      });
    },

    handleClose(done) {
      if(this.form.isDetail){
        this.$confirm("确认关闭？")
          .then((_) => {
            this.reset();
            done();
          })
          .catch((_) => {});
      }else{
        this.reset();
        done();
      }
    },
  },
};
</script>
<style>
.label-header-msg {
  font-weight: bold;
}

.form-card {
  margin-bottom: 10px;
}
</style>
